<?php

namespace App\Admin\Controllers;

use App\Admin\Renderable\FirmTable;
use App\Admin\Repositories\FirmBrand;
use App\Models\BrandCradle;
use App\Models\BrandLocation;
use App\Models\Firm;
use App\Models\FirmProfession;
use App\Models\Message;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class FirmBrandController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new FirmBrand(['firm:id,name','profession:id,name','cradle:id,name','location:id,name']), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('firm.name','所属企业');
            $grid->column('name');
            $grid->column('profession.name','品牌行业');
            $grid->column('cradle.name','品牌发源地');
            $grid->column('location.name','品牌定位');
            $grid->column('audit_status')
                 ->using([1 => '待审核',2 =>'已通过',3 => '已拒绝'])
                 ->label([1 => 'danger',2 => 'success',3 => 'warning']);
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();
            $grid->fixColumns(1);

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();

                $filter->equal('firm.firm_id','企业名称')
                       ->width(3);

                $filter->like('name')
                       ->width(3);

                $filter->equal('firm_profession_id')
                    ->select(FirmProfession::getOption())
                    ->width(3);
                $filter->equal('brand_cradle_id')
                    ->select(BrandCradle::getOption())
                    ->width(3);
                $filter->equal('brand_location_id')
                    ->select(BrandLocation::getOption())
                    ->width(3);

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new FirmBrand(), function (Show $show) {
            $show->field('id');
            $show->field('firm_id');
            $show->field('name');
            $show->field('certificate');
            $show->field('firm_profession_id');
            $show->field('brand_cradle_id');
            $show->field('brand_location_id');
            $show->field('audit_status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new FirmBrand(['profile']), function (Form $form) {
            $form->display('id');
            $form->selectTable('firm_id','企业')
                 ->from(FirmTable::make())
                 ->model(Firm::class,'id','name')
                 ->required();
            $form->text('name')
                 ->maxLength(100)
                 ->required();
            $form->image('certificate')
                 ->disk('oss')
                 ->autoUpload()
                 ->removable(false)
                 ->uniqueName()
                 ->saveFullUrl()
                 ->required();
            $form->select('firm_profession_id')
                 ->options(FirmProfession::getOption())
                 ->required();
            $form->select('brand_cradle_id')
                 ->options(BrandCradle::getOption())
                 ->required();
            $form->select('brand_location_id')
                 ->options(BrandLocation::getOption())
                 ->required();


            $form->datetime('consult_time','咨询时间')
                ->customFormat(function ($value){
                    if ($value && $value>0){
                        return date('Y-m-d H:i:s',$value);
                    }

                    return  null;
                })->saving(function ($value){
                    if ($value){
                        return strtotime($value);
                    }
                    return $value;
                })->required();

            $form->multipleImage('profile.banners','轮播图')
                ->disk('oss')
                ->autoUpload()
                ->removable(false)
                ->uniqueName()
                ->saveFullUrl()
                ->saving(function ($value){
                    return  empty($value) ? null : $value;
                });

            $form->file('profile.video_url','视频')
                ->disk('oss')
                ->autoUpload()
                ->removable(false)
                ->uniqueName()
                ->saveFullUrl()
                ->saving(function ($value){
                    return (string)$value;
                });


            $form->display('profile.video_url','浏览视频')
                ->customFormat(function ($value){
                    $html = '<video width="320" height="240" controls>
    <source src="'.$value.'" type="video/mp4">
</video>';
                    return $html;
                });

            $form->editor('profile.intro','品牌介绍')
                 ->saving(function ($value){
                     return (string)$value;
                 });

            $form->editor('profile.advantage','品牌优势')
                ->saving(function ($value){
                    return (string)$value;
                });


            $form->radio('profile.is_invest_price','投资是否区域议价')
                 ->options([1 => '是',2  => '否'])
                 ->when(2,function (Form $form){
                     $form->range('profile.start_invest_amount','profile.end_invest_amount','投资金额')
                          ->rules(['profile.end_invest_amount' =>  'required_if:is_area_price,2'],[
                              'required_if' => '请填写投资金额'
                          ]);

                 })->required();


            $form->radio('profile.is_join_price','加盟是否区域议价')
                ->options([1 => '是',2  => '否'])
                ->when(2,function (Form $form){
                    $form->range('profile.start_join_in_fee','profile.end_join_in_fee','加盟费用')
                        ->rules('required_if:is_area_price,2',[
                            'required_if' => '请填写加盟费用'
                        ]);

                })->required();


            $form->number('profile.store_num','门店数量')
                 ->min(0)
                ->saving(function ($value){
                    return (int)$value;
                });


            $form->text('profile.set_up_time','成立时间')
                ->maxLength(60)
                ->saving(function ($value){
                    return (string)$value;
                });

            $form->list('profile.cooperation_area','合作区域')
                ->saving(function ($value){
                    if ($value){
                        return (array)$value;
                    }
                    return  $value;
                });

            $form->text('profile.manage_method','管理方式')
                ->maxLength(60)
                ->saving(function ($value){
                    return (string)$value;
                });

            $form->text('profile.invest_crowd','投资人群')
                ->maxLength(60)
                ->saving(function ($value){
                    return (string)$value;
                });


            $form->list('profile.tags','品牌标签')
                 ->saving(function ($value){
                     if ($value){
                         return (array)$value;
                     }
                     return  $value;
                 });

            $form->image('profile.cover_one','1:1封面图')
                ->disk('oss')
                ->autoUpload()
                ->removable(false)
                ->uniqueName()
                ->saveFullUrl()
                ->saving(function ($value){
                    return (string)$value;
                });


            $form->image('profile.cover_two','4:3封面图')
                ->disk('oss')
                ->autoUpload()
                ->removable(false)
                ->uniqueName()
                ->saveFullUrl()
                ->saving(function ($value){
                    return (string)$value;
                });

            $form->table('profile.contact','联系信息',function ($table){
                $table->text('area','区域');
                $table->text('name','姓名');
                $table->text('phone','电话');
            });

            $form->radio('audit_status')
                 ->options([1 => '待审核',2 => '已通过',3 => '已拒绝'])
                 ->required();

            $form->saving(function (Form $form){
                Message::query()->create([
                    'type' => 1,
                    'content' => $form->input('name'). '品牌成功入驻了花石木平台'
                ]);
            });


            $form->saved(function (Form $form){

                if ($form->input('user_id') && $form->input('audit_status')==2){
                    event(new \App\Events\Message([
                        'user_id' =>  $form->input('user_id'),
                        'type' => 2,
                        'content' => '恭喜您申请的品牌入驻已通过审核'
                    ]));
                }

                if ($form->input('user_id') && $form->input('audit_status')==3){
                    event(new \App\Events\Message([
                        'user_id' =>  $form->input('user_id'),
                        'type' => 2,
                        'content' => '您申请的品牌入驻未通过审核'
                    ]));
                }
            });

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
